package net.atomarrow.sy.base;

import java.io.Serializable;
import java.util.List;

import net.atomarrow.bean.Pager;
import net.atomarrow.db.parser.Conditions;
import net.atomarrow.services.Service;
import net.atomarrow.sy.form.Form;
import net.atomarrow.util.StringUtil;

public abstract class BaseService extends Service {
    protected abstract String getDomainName();
    protected Conditions getConditions() {
        return new Conditions(getDomainName());
    }
    protected void processOrder(Form form, Conditions conditions) {
        if (form != null && form.getOrderColumnNum()!=null&&form.getOrderColumnNum() > 0
                && StringUtil.isNotBlank(form.getOrderColumnName())
                && StringUtil.isNotBlank(form.getOrderDir())) {
            if ("asc".equals(form.getOrderDir())) {
                conditions.putASC(form.getOrderColumnName());
            } else if("desc".equals(form.getOrderDir())) {
                conditions.putDESC(form.getOrderColumnName());
            }
        }
    }
    protected int modifyColumnById(Integer id, Serializable... columns) {
        return modifyWithColumn(getConditions().putEW("id", id), columns);
    }

    protected <T>List<T> getList(Serializable... columns) {
        Conditions conditions = getConditions();
        for (int i = 0; i < columns.length; i += 2) {
            conditions.putEW(columns[i], columns[i + 1]);
        }
        return getList(conditions);
    }

    protected <T>List<T> getListByPage(Pager pager,
            Serializable... columns) {
        Conditions conditions = getConditions();
        for (int i = 0; i < columns.length; i += 2) {
            conditions.putEW(columns[i], columns[i + 1]);
        }
        pager.setDataTotal(getCount(conditions));
        return getListByPage(conditions, pager);
    }
}
